<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    class="flex"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :disabled="baTable.form.operate == 'view'"
                    :rules="rules"
                >
                <FormItem :label="t('xStockin.PJIN')" type="string" v-model="baTable.form.items!.PJIN" prop="PJIN" :input-attr="{ placeholder: '保存时自动生成', disabled: true }" />
                <FormItem :label="t('xStockin.FITTING')" type="string" v-model="baTable.form.items!.FITTING" prop="FITTING" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.FITTING') }) }" />
                <FormItem :label="t('xStockin.PRICE')" type="number" prop="PRICE" v-model.number="baTable.form.items!.PRICE" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('xStockin.PRICE') }) }" />
                <FormItem :label="t('xStockin.PIECES')" type="number" prop="PIECES" v-model.number="baTable.form.items!.PIECES" :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('xStockin.PIECES') }) }" />
                <FormItem :label="t('xStockin.SPECS')" type="string" v-model="baTable.form.items!.SPECS" prop="SPECS" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.SPECS') })}" />
                <FormItem :label="t('xStockin.STOCKAREA')" type="string" v-model="baTable.form.items!.STOCKAREA" prop="STOCKAREA" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.STOCKAREA') }) }" />
                <FormItem :label="t('xStockin.SYR')" type="string" v-model="baTable.form.items!.SYR" prop="SYR" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.SYR') }) }" />
                <FormItem :label="t('xStockin.RKDATE')" type="datetime" v-model="baTable.form.items!.RKDATE" prop="RKDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xStockin.RKDATE') }) }" />
                <FormItem :label="t('xStockin.STOCK')" type="number" prop="STOCK" v-model.number="baTable.form.items!.STOCK" :input-attr="{ step: '1', placeholder: '', disabled: true }" />
                <FormItem :label="t('xStockin.BRAND')" type="string" v-model="baTable.form.items!.BRAND" prop="BRAND" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.BRAND') })}" />
                <FormItem :label="t('xStockin.SUPPLIER')" type="string" v-model="baTable.form.items!.SUPPLIER" prop="SUPPLIER" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.SUPPLIER') })}" />
                <FormItem :label="t('xStockin.DEPT')" 
                v-model="baTable.form.items!.DEPT" 
                prop="DEPT" 
                type="remoteSelect"
                :input-attr="{
                    pk:'DEPTID',
                    field: 'DEPTNAME',
                    'remote-url':dEPARTMENT +'index',
                    placeholder: t('Click Select'),
                }" 
                />
                <FormItem :label="t('xStockin.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: '当前登录人', disabled: true }" />
                <FormItem :label="t('xStockin.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前时间', disabled:true }" />
                <FormItem :label="t('xStockin.REMARK')" style="width: 100%" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('xStockin.REMARK') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { dEPARTMENT , corplib , codelib , authAdmin} from '/@/api/controllerUrls'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    FITTING: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.FITTING') })})],
    PRICE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.PRICE') })})],
    PIECES: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.PIECES') })})],
    SPECS: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.SPECS') })})],
    STOCKAREA: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.STOCKAREA') })})],
    RKDATE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xStockin.RKDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xStockin.OPTIME') })})],
})

</script>

<style scoped lang="scss"></style>
